<?php
/**
 * 通过主键，获取单个item
 * author: JiangJun
 * CreateTime:2021/7/25 22:28
 */


require '../vendor/autoload.php';

use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\DynamoDb\Marshaler;

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest'
]);

$dynamodb = $sdk->createDynamoDb();
$marshaler = new Marshaler();

$tableName = 'Movies';

$year = 2015;
$title = 'The Big New Movie';

//官方的例子，指定的主键
$key = $marshaler->marshalJson('
    {
        "year": ' . $year . ', 
        "title": "' . $title . '"
    }
');

//我自己写的例子,对于getItem来说，如果表中有partion key 和 sort
$jjKey=$marshaler->marshalItem([
    'year'=>2021,
    'title'=>'Jiang Jun',
]);

$params = [
    'TableName' => $tableName,
    'Key' => $key,
    'ReturnConsumedCapacity'=>'TOTAL'//返回消耗
];

try {
    $result = $dynamodb->getItem($params);
    print_r($result);

} catch (DynamoDbException $e) {
    echo "Unable to get item:\n";
    echo $e->getMessage() . "\n";
}